package com.example.csetpp.rabbitmq;


import com.example.csetpp.pojo.dto.messagequeue.MailMessage;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import static com.example.csetpp.constants.RabbitMqConstant.*;

@Component
@Transactional
public class Listener {

    @Autowired
    private JavaMailSender sender;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MAIL_QUEUE,durable = "true"),
            exchange = @Exchange(value = MAIL_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = MAIL_QUEUE_KEY))
    public void listenOrderPayStatus(MailMessage msg) throws IllegalAccessException {
        if(msg == null || msg.getMail() == null || msg.getCode() == null){
            return;
        }

        String code = msg.getCode();
        String mail = msg.getMail();
        SimpleMailMessage mailCode = new SimpleMailMessage();
        mailCode.setSubject("邮箱验证码");
        mailCode.setText(code);
        mailCode.setTo(mail);
        mailCode.setFrom("2207785922@qq.com");
        sender.send(mailCode);

    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MAIL_QUEUE,durable = "true"),
            exchange = @Exchange(value = MAIL_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = MAIL_QUEUE_KEY))
    public void listenOrderPayStatus2(MailMessage msg) throws IllegalAccessException {
        if(msg == null || msg.getMail() == null || msg.getCode() == null){
            return;
        }

        String code = msg.getCode();
        String mail = msg.getMail();
        SimpleMailMessage mailCode = new SimpleMailMessage();
        mailCode.setSubject("邮箱验证码");
        mailCode.setText(code);
        mailCode.setTo(mail);
        mailCode.setFrom("2207785922@qq.com");
        sender.send(mailCode);

    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MAIL_QUEUE,durable = "true"),
            exchange = @Exchange(value = MAIL_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = MAIL_QUEUE_KEY))
    public void listenOrderPayStatus3(MailMessage msg) throws IllegalAccessException {
        if(msg == null || msg.getMail() == null || msg.getCode() == null){
            return;
        }

        String code = msg.getCode();
        String mail = msg.getMail();
        SimpleMailMessage mailCode = new SimpleMailMessage();
        mailCode.setSubject("邮箱验证码");
        mailCode.setText(code);
        mailCode.setTo(mail);
        mailCode.setFrom("2207785922@qq.com");
        sender.send(mailCode);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MAIL_QUEUE,durable = "true"),
            exchange = @Exchange(value = MAIL_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = MAIL_QUEUE_KEY))
    public void listenOrderPayStatus4(MailMessage msg) throws IllegalAccessException {
        if(msg == null || msg.getMail() == null || msg.getCode() == null){
            return;
        }

        String code = msg.getCode();
        String mail = msg.getMail();
        SimpleMailMessage mailCode = new SimpleMailMessage();
        mailCode.setSubject("邮箱验证码");
        mailCode.setText(code);
        mailCode.setTo(mail);
        mailCode.setFrom("2207785922@qq.com");
        sender.send(mailCode);

    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MAIL_QUEUE,durable = "true"),
            exchange = @Exchange(value = MAIL_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = MAIL_QUEUE_KEY))
    public void listenOrderPayStatus5(MailMessage msg) throws IllegalAccessException {
        if(msg == null || msg.getMail() == null || msg.getCode() == null){
            return;
        }

        String code = msg.getCode();
        String mail = msg.getMail();
        SimpleMailMessage mailCode = new SimpleMailMessage();
        mailCode.setSubject("邮箱验证码");
        mailCode.setText(code);
        mailCode.setTo(mail);
        mailCode.setFrom("2207785922@qq.com");
        sender.send(mailCode);

    }
}
